/**
 * Created by hp on 2018/11/15.
 */
$(function () {
    $("#province").cityPicker({
        title: "选择城市",
        toolbarCloseText: "完成",
        onChange: function (picker, values, displayValues) {
            $("#city").val(values[1]);
            $("#district").val(values[2]);
            $("#projectName").val("");
            $("#building").val("");
            $("#unit").val("");
            $("#roomNumber").val("");
            $("#area").val("");
            $(".projectNameList").hide();
            $(".buildingList").hide();
            $(".unitList").hide();
            $(".houseList").hide();
        }
    });
    function getAddress() {
        $.ajax({
                type: "POST",
                url: "/wechatOpportunity/wxGetAddressDetails",
                data: {
                    projectName: $("#projectName").val(),
                    city: $("#city").val(),
                    district: $("#district").val(),
                    tag: $("#tag").val(),
                },

                success: function (data) {
                    if (data.status == "success") {
                        var houseList = data.houseList;
                        if (houseList) {
                            $(".floorList").html("");
                            $(".floorList").show();
                            $(".houseList").html("");
                            for (var key in houseList) {
                                var option = '<p class="item">' + key + '层</p>';
                                $(".floorList").append(option);
                                $("#tag").val("floorList");
                                for (var i in houseList[key]) {
                                    var content = '<p class="item" dataBuildingArea="' + houseList[key][i].buildingArea + '">' + houseList[key][i].house + '</p>';
                                }


                            }

                        }
                        else {
                            $(".projectNameListDiv").append("<h4 class='alertInfo'>未找到对应的小区，请手动输入！</h4>");
                        }


                        var unitList = data.unitList;
                        if (unitList) {
                            $(".unitList").attr("style", "display:show;");
                            var content;
                            $('.unitList').html("");
                            for (var i = 0; i < unitList.length; i++) {
                                var content = '<p class="item" dataUnitId="' + unitList[i].unitId + '">' + unitList[i].unitName + '</p>';
                                $(".unitList").append(content);
                            }

                        }

                        var buildingList = data.buildingList;
                        if (buildingList) {
                            $(".buildingList").attr("style", "display:show;");
                            var content;
                            $('.buildingList').html("");
                            for (var i = 0; i < buildingList.length; i++) {
                                var content = '<p class="item" dataBuildingId="' + buildingList[i].buildingId + '">' + buildingList[i].buildingName + '</p>';
                                $(".buildingList").append(content);
                            }

                        }


                        var projectNameList = data.projectNameList;

                        if (projectNameList) {
                            $(".projectNameList").attr("style", "display:show;");
                            $(".projectNameList").html("");
                            var content = '';

                            for (var i = 0; i < projectNameList.length; i++) {
                                content += '<div class="weui-media_box weui-media_text projectNameItem">';
                                content += '<h4 class="weui-media_title">' + projectNameList[i].communityName + '</h4>';
                                content += '<p class="weui-media_desc">' + projectNameList[i].address + '</p>';
                                content += '<span class="hide">' + projectNameList[i].communityId + '</span>';
                                content += '</div>'

                            }
                            $(".projectNameList").append(content);

                        }
                        $("#tag").val(data.addressName)
                    }
                    else if (data.status == "failed") {
                        var tagValue = $("#tag").val();
                        if (tagValue == "projectNameList") {
                            $(".projectNameListDiv .alertInfo").text("未匹配到信息，请手动输入").show();

                        }
                        if (tagValue == "buildingList") {
                            $(".projectNameListDiv .alertInfo").text("未匹配到信息，请手动输入").show();

                        }
                        if (tagValue == "unitList") {
                            $(".projectNameListDiv .alertInfo").text("未匹配到信息，请手动输入").show();

                        }
                        if (tagValue == "floorList") {
                            $(".projectNameListDiv .alertInfo").text("未匹配到信息，请手动输入").show();

                        }
                    }
                    else {
                        console.log("获取地址返回异常：" + data.errorMsg);
                    }
                },
                error: function () {
                    console.log("获取地址数据请求失败");
                }
            }
        );
        return

    }

    $("#area").focus(function () {
        if ($("#projectName").val().trim()) {
            $(".projectNameListDiv .alertInfo").hide();
        }
    });

// 动态获取小区
    $("#projectName").bind('input propertychange', function () {
        $("#communityId").val("");
        $("#building").val("");

        $("#area").val("");

        $(".projectNameList").hide();
        $(".buildingList").hide();
        $(".unitList").hide();
        $(".floorList").hide();
        $(".houseList").hide();
        if ($(this).val().trim()) {
            getAddress();
        }

    });

// 动态获取楼栋
    $(".projectNameList").delegate(" .projectNameItem", "click", function () {
        var housecell = $(this).children("h4").text().trim();
        $("#projectName").val(housecell);
        var communityId = $(this).children("span").text().trim();
        $("#communityId").val(communityId);
        $(".projectNameList").hide();
        getAddress();
    });
    $(".projectNameList").delegate(" h4", "click", function () {
        $(".projectNameList").hide();
    });


//动态获取单元
    $(".buildingList").delegate(" .item", "click", function () {
        $("#projectName").val($("#tag").val() + $(this).text().trim());
        $("#building").val($(this).text().trim());
        $("#buildingId").val($(this).attr("dataBuildingId").trim());
        $(".buildingList").hide();
        getAddress();
    });

//动态获取户
    $(".unitList").delegate(" .item", "click", function () {
        $("#projectName").val($("#tag").val() + $(this).text().trim());
        $("#unit").val($(this).text().trim());
        $("#unitId").val($(this).attr("dataUnitId").trim());
        $(".unitList").hide();
        getAddress();
    });


    $(".floorList").delegate(" .item", "click", function () {
        var floor = $(this).text().trim();
        $("#floor").val(floor);
        $(".floorList").hide();
        var totalHouse = $(".houseList .item");
        for (var i = 0; i < totalHouse.length; i++) {
            if (floor != totalHouse.eq(i).text().trim().substring(0, 1)) {
                totalHouse.eq(i).remove();
                $(".houseList").show();
            }
        }
    });

    $(".houseList").delegate(" .item", "click", function () {
        $("#roomNumber").val($(this).text().trim());
        $("#area").val($(this).attr("dataBuildingArea").trim())
        $(".houseList").hide();
    });

    $("#wxcreateStep3Btn").click(function () {

        // 所属城市
        var city = $("#city").val().trim();
        if (!city) {
            helpMessage("请输入所在城市");
            return;
        }

        // 所在区县
        var district = $("#district").val().trim();
        if (!district) {
            helpMessage("请输入所在区县");
            return;
        }

        // 所在小区
        var projectName = $("#projectName").val().trim();
        if (!projectName) {
            helpMessage("请输入所在小区");
            return;
        }

        // 面积
        var area = $("#area").val().trim();
        if (!area) {
            helpMessage("请输入住宅面积");
            return;
        }
        if (area <= 0) {
            helpMessage("住宅面积必须大于0");
            return;
        }


        // 询价并提交表单
        $('#wxcreateStep3Btn').addClass("btn_disabled").attr('disabled', 'disabled');
        $.ajax({
            type: "POST",
            url: "/leads/wxQueryPrice",
            data: {
                city: city,
                district: district,
                projectName: projectName,
                area: area

            },
            beforeSend: function () {
                $(".helpMsg").html("");
                var html = '<h4 class="msg1">评房信息已提交</h4>' + '<h5 class="msg2">系统正在评估中，请稍等 <span class="colorRed">15-30s</span>，返回评房结果</h5>';
                $(html).appendTo($(".helpMsg"));
                $(".helpMsg").fadeIn(200);
            },
            success: function (data) {
                if (data.status == "success") {
                    var price = data.price
                    $("#unitPrice").val(price["unitprice"]);
                    $("#status").val(data["collateralStatus"]);
                    $("#externalId").val(price["evaluateCode"]);
                    $("#wxCreate2Form3").submit();
                    $(".helpMsg").hide();
                } else {
                    helpMessage(data.errorMsg);
                    return;
                }
            }
        });
    });

    setTimeout(function () {
        $(".message").fadeOut(200);
    }, 2000);

    function helpMessage(message) {
        $(".helpMsg").text(message).fadeIn(200);
        setTimeout(function () {
            $(".helpMsg").fadeOut(200);
        }, 2000);
    }

})
;